<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品 列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区 -->
    <el-card>
      <el-row :gutter="20">
        <!--输入框 -->
        <el-col :span="8">
          <el-input
            ref="searchref"
            placeholder="请输入内容"
            v-model="queryData.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchs"
            ></el-button>
          </el-input>
        </el-col>
        <!-- 添加按钮 -->
        <el-col :span="16">
          <el-button type="primary" @click="addjump">添加商品</el-button>
        </el-col>
      </el-row>
    </el-card>
    <!-- 商品列表区 -->
    <el-table :data="goodsList" border stripe>
      <el-table-column type="index"></el-table-column>
      <el-table-column label="商品名称" prop="goods_name"></el-table-column>
      <el-table-column
        label="商品价格(元)"
        prop="goods_price"
        width="120px"
      ></el-table-column>
      <el-table-column
        label="商品重量(kg)"
        prop="goods_weight"
        width="170px"
      ></el-table-column>
      <el-table-column label="创建时间" prop="add_time" width="240px">
        <template slot-scope="scope">
          <!-- 过滤时间 -->
          {{ scope.row.add_time | dataFormat }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="130px">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
          <!-- 删除按键 -->
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="Dodel(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      :page-sizes="[2, 3, 5, 10]"
      :page-size="queryData.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  created () {
    this.getGoodsList()
  },
  data () {
    return {
      queryData: {
        query: '', // 查询关键字
        pagenum: 1, // 当前页码
        pagesize: 5 // 当前页面容纳数据
      },
      // 商品列表数据
      goodsList: [],
      // 总商品数量
      total: 0

    }
  },
  methods: {
    // 请求并渲染数据
    async getGoodsList () {
      const { data: resData } = await this.$http.get('goods', {
        params: this.queryData
      })
      if (resData.meta.status !== 200) return this.$message.error(resData.meta.msg)
      // 如果成功设置商品数据列表 和总数量
      this.goodsList = resData.data.goods
      this.total = resData.data.total
      // console.log(resData)
    },
    // 页容量改变事件
    handleSizeChange (newsize) {
      this.queryData.pagesize = newsize
      this.getGoodsList()
    },
    // 页码改变事件
    handleCurrentChange (newpage) {
      this.queryData.pagenum = newpage
      this.getGoodsList()
    },
    // 搜索
    searchs () {
      if (this.queryData.query.trim().length === 0) {
        // 如果输入的内荣为空 就让文本框获取焦点
        this.$refs.searchref.focus()
        return this.$message.warning('请输入~~~')
      }
      this.getGoodsList()
    },
    // 删除
    async Dodel (rowData) {
      // 获取删除接口id
      const delid = rowData.goods_id
      const { data: resData } = await this.$http.delete(`goods/${delid}`)
      // 失败
      if (resData.meta.status !== 200) return this.$message.error(resData.meta.msg)
      // 成功
      this.$message.success(resData.meta.msg)
      this.getGoodsList()
    },
    // 点击添加跳转商品页面
    addjump () {
      this.$router.push('/goods/add')
    }

  }

}
</script>

<style lang="postcss" scoped>
</style>
